//
//  HLCellLayoutConfig.m
//  YueYue
//
//  Created by hulu001 on 2023/6/12.
//

#import "HLCellLayoutConfig.h"
#import "HLCustomContentConfig.h"
#import "HLChatKit.h"

@interface HLCellLayoutConfig ()

@property (nonatomic, strong) NSArray *types;
@property (nonatomic, strong) HLCustomContentConfig *customContentConfig;

@end

@implementation HLCellLayoutConfig


- (instancetype)init
{
    self = [super init];
    if (self) {
        _types =  @[
                   @"HLAccostAttachment",
                   @"HLGiftAttachment",
                   @"HLPushAttachment",
                   @"HLVideoGuideAttachment",
                   @"HLRechargeAttachment",
                   @"HLPayRemindAttachment",
                   @"HLOtherRemidAttach"
                   ];
        _customContentConfig = [[HLCustomContentConfig alloc] init];
    }
    return self;
}

#pragma mark - IMCellLayoutConfig

- (CGSize)contentSize:(HLIMKitMessageModel *)model cellWidth:(CGFloat)width
{
    if ([self isSupportedCustomMessage:model.message]) {
        return [_customContentConfig contentSize:width message:model.message];
    }
    return [super contentSize:model cellWidth:width];
}

/// 需要构造的cellContent类名
- (NSString *)cellContent:(HLIMKitMessageModel *)model
{
    if ([self isSupportedCustomMessage:model.message]) {
        return [_customContentConfig cellContent:model.message];
    }
    
    return [super cellContent:model];
}

/// 左对齐的气泡，气泡距离整个cell的内间距
- (UIEdgeInsets)cellInsets:(HLIMKitMessageModel *)model
{
    if ([self isSupportedCustomMessage:model.message]) {
        return [_customContentConfig cellInsets:model.message];
    }
    
    return [super cellInsets:model];
}

/// 左对齐的气泡，气泡里内容距离气泡的内间距
- (UIEdgeInsets)contentViewInsets:(HLIMKitMessageModel *)model
{
    if ([self isSupportedCustomMessage:model.message]) {
        return [_customContentConfig contentViewInsets:model.message];
    }
    
    return [super contentViewInsets:model];
}

- (BOOL)shouldShowAvatar:(HLIMKitMessageModel *)model
{
    if ([self isSupportedCustomMessage:model.message]) {
        return [_customContentConfig showAvatar:model.message];
    }
    
    return [super shouldShowAvatar:model];
}

- (BOOL)shouldShowUnread:(HLIMKitMessageModel *)model
{
    if ([self isSupportedCustomMessage:model.message]) {
        return YES;
    }
    
    return [super shouldShowUnread:model];
}

- (BOOL)isSupportedCustomMessage:(NIMMessage *)message
{
    NIMCustomObject *object = (NIMCustomObject *)message.messageObject;
    return [object isKindOfClass:[NIMCustomObject class]] &&
    [_types indexOfObject:NSStringFromClass([object.attachment class])] != NSNotFound;
}

@end
